import { getDicts } from "@/api/system/dict/data";
import store from '@/store';

export const RvaUtils = {
  clone(src, target, deep) {
    if (!target) {
      target = {};
    }
    if (!src) {
      return target;
    }
    if (deep) {
      src = JSON.parse(JSON.stringify(src));
    }
    for (var key in src) {
      target[key] = src[key];
    }
    return target;
  },
  cloneDeep (obj) {
    if (RvaUtils.isEmpty(obj)) {
      return null;
    }
    return JSON.parse(JSON.stringify(obj));
  },
  isEmpty(obj) {
    return obj == null || obj == undefined || obj == '' || obj.length == 0;
  },
  isNotEmpty (obj){
    return !RvaUtils.isEmpty(obj);
  },
  isObject (obj) {
    return (typeof obj === 'object');
  },
  parseValue (jsonStringOrObject, key, defaultValue) {
    if (jsonStringOrObject) {
      var obj;
      if (typeof jsonStringOrObject === Object) {
        obj = jsonStringOrObject;
      } else {
        obj = JSON.parse(jsonStringOrObject);
      }
      if (key in obj) {
        return obj[key];
      }
      return defaultValue;
    } else {
      return defaultValue;
    }
  },
  parseInt (val, defaultVal) {
    if (isNaN(val)) {
      return defaultVal;
    }
    return parseInt(val);
  },
  parseFloat (val, defaultVal) {
    if (isNaN(val)) {
      return defaultVal;
    }
    return parseFloat(val);
  },
  getValidateRule(rule) {
    let rules = {
      IDCard: /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$/,
      mobile: /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/
    };
    if (rules[rule]) {
      return rules[rule];
    }
    return eval(rule);
  },
  endsWith (string, suffix) {
    return new RegExp(suffix + "$").test(string);
  },
  getKeyBySuffix (obj, suffix) {
    for (var key in obj) {
      if (RvaUtils.endsWith(key, suffix)) {
        return key;
      }
    }
  },
  getValueBySuffix (obj, suffix) {
    let key = this.getKeyBySuffix (obj, suffix);
    if (key) {
      return obj[key];
    }
  },
  trigger (formData, formDataOld, viewData, propRefs, rowIndex) {
    // 使用说明请参考：https://gitee.com/jiaodacailei/RuoYi-Vue-Auto/issues/I47HU1
    function getSuffix (suffix) {
      if (rowIndex) {
        suffix += '_' + rowIndex;
      }
      return suffix;
    }

    function getPropBySuffix (suffix) {
      return viewData.properties.filter(p => RvaUtils.endsWith(p.id, getSuffix(suffix)))[0];
    }

    function hide(suffix) {
      let prop = getPropBySuffix (suffix);
      if (prop) {
        prop.originalType = prop.type;
        prop.type = 'hidden';
      }
    }

    function show(suffix) {
      let prop = getPropBySuffix (suffix);
      if (prop) {
        prop.type = prop.originalType;
        rerenderForm()
      }
    }

    function getPropRef (suffix) {
      return  RvaUtils.getValueBySuffix(propRefs, getSuffix(suffix))[0];
    }


    let triggers = viewData.triggers;
    if (!triggers || triggers.length == 0) {
      return;
    }
    for (var i in triggers) {
      let trigger = triggers[i];
      console.log ('trigger', trigger);
      // 获取参数值
      let params = [];
      let paramsOld = [];
      for (var j = 0; j < trigger.params.split(',').length; j ++) {
        let key = RvaUtils.getKeyBySuffix(formData, trigger.params.split(',')[j]);
        // console.log (key);
        if (key) {
          if (RvaUtils.isNotEmpty(formData[key])) {
            params.push(formData[key]);
          } else {
            params.push(null);
          }
          if (formDataOld) {
            if (RvaUtils.isNotEmpty(formDataOld[key])) {
              paramsOld.push(formDataOld[key]);
            } else {
              paramsOld.push(null);
            }
          }
        } else {
          params.push(null);
          if (formDataOld) {
            paramsOld.push(null);
          }
        }
      }
      // console.log ('trigger params', params, paramsOld);
      // params是否有值变化，有变化时，才触发action
      let change = false;
      if (params.length == paramsOld.length) {
        for (var j = 0; j < params.length; j ++) {
          if (RvaUtils.isEmpty(params[j]) !== RvaUtils.isEmpty(paramsOld[j])) {
            change = true;
            continue;
          }
          if (RvaUtils.isEmpty(params[j]) && RvaUtils.isEmpty(paramsOld[j])) {
            continue;
          }
          if (typeof params[j].sort == "function") {
            if (params[j].sort().toString() !== paramsOld[j].sort().toString()) {
              change = true;
            }
          } else if (params[j].toString() != paramsOld[j].toString()) {
            change = true;
          }
        }
      } else {
        change = true;
      }
      if (!change) {
        continue;
      }
      if (!eval(trigger.triggerIf)) {
        continue;
      }
      console.log ('trigger', trigger);
      // 触发action
      for (var a = 0; a < trigger.actions.length; a ++) {
        let action = trigger.actions[a];
        let propRef = getPropRef(action.actionSubject);
        if (RvaUtils.isEmpty(action.actionParams)) {
          propRef[action.action]();
        } else {
          propRef[action.action](action.actionParams);
        }
      }
    }
  },
  parseValidateRules (prop, rules) {
    if (!rules) {
      rules = [];
    }
    //
    if (prop.formRequired == 'Y') {
      rules.push({required: true, message: prop.name + '为必需'});
    }
    if (prop.type == 'textfield') {
      let min = RvaUtils.parseInt(prop.formValueMin, 0);
      if (min < 0) {
        min = 0;
      }
      let max = RvaUtils.parseInt(prop.formValueMax, 20);
      rules.push({
        pattern: eval ('/^.{' + min + ',' + max + '}$/'),
        message: '只能输入' + min + '-' + max + '个字符'
      });
      let validateRule = RvaUtils.parseValue(prop.data, 'validateRule');
      if (!validateRule) {
        return rules;
      }
      if (validateRule == 'email') {
        rules.push({
          type: 'email',
          message: 'email格式不正确'
        });
      } else {
        let regex = RvaUtils.getValidateRule(validateRule);
        rules.push({
          pattern: regex,
          message: RvaUtils.parseValue(prop.data, 'validateErrorMessage')
        });
      }
    }
    return rules;
  },
  getDicts (dictType, obj, objProp) {
    if (RvaUtils.isEmpty(dictType)) {
      return;
    }
    if (!objProp) {
      objProp = 'dictData';
    }
    let key = 'rva.dict:' + dictType;
    if (sessionStorage.getItem(key) == 'processing') {
      setTimeout(() => RvaUtils.getDicts(dictType, obj, objProp), 100);
      return;
    }
    if (sessionStorage.hasOwnProperty(key)) {
      obj[objProp] = JSON.parse(sessionStorage.getItem(key));
    } else {
      sessionStorage.setItem(key, 'processing');
      getDicts(dictType).then(response => {
        sessionStorage.setItem(key, JSON.stringify(response.data));
        obj[objProp] = response.data;
      });
    }
  },
  isDevMode () {
    return store.state.user.roles.indexOf('admin') >= 0 && sessionStorage.getItem('rvaDevMode') == 'Y';
  },
  getColor (color) {
    let data = {
      primary :'#409EFF',
      info :'#909399',
      success :'#67C23A',
      warning :'#E6A23C',
      danger :'#F56C6C'
    };
    return data[color];
  }
};
